using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DMClass objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMClass.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    [TestFixture]
    public class TestDMClassMustHaveOneProp
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMClassMustHaveOneProp

        [Test]
        public void Test_CreateDMClassMustHaveOnePropRule()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMClassMustHaveOneProp();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("ClassMustHaveOneProp", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
        }

//        [Test]
//        public void Test_CreateDMClassMustHaveOnePropRule_WhenClassNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMClassMustHaveOneProp(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//                //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmClass", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_DMClassMustHaveOneProp_FailsIfNoProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            IBusinessObjectRule rule = new DMClassMustHaveOneProp();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' must have at least one Property", rule.Message);
        }

        [Test]
        public void Test_DMClassMustHaveOneProp_PassesIfOneProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            dmClass.Properties.CreateBusinessObject();
            IBusinessObjectRule rule = new DMClassMustHaveOneProp();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_DMClassMustHaveOneProp_Passes_WhenHasNoProps_AndHasSuperClassWithProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMClass superClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            DMInheritanceRelationship inheritanceRelationship = new DMInheritanceRelationship();
            inheritanceRelationship.SuperClass = superClass;
            inheritanceRelationship.SubClass = dmClass;
            superClass.Properties.CreateBusinessObject();
            IBusinessObjectRule rule = new DMClassMustHaveOneProp();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClass.Properties.Count);
            Assert.AreEqual(1, superClass.Properties.Count);
            Assert.IsNotNull(dmClass.InheritanceSuperClassRelationship);
            Assert.AreSame(superClass, dmClass.InheritanceSuperClassRelationship.SuperClass);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        #endregion


    }

}